<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class FindPasswordForm extends CFormModel {

    public $email;
    public $verifyCode;
    public $model;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('UserName', 'required'),
            array('UserName', 'checkUserName'),
            array('verifyCode', 'checkVerify'),
        );
    }

    public function checkUserName(){
        if(is_numeric($this->UserName) && strlen($this->UserName) == 11){
            $this->model = MemberModel::model()->find(BaseModel::getC(array('Phone' => $this->UserName)));  
        }else if(strpos($this->UserName, '@') !== false){
            $this->model = MemberModel::model()->find(BaseModel::getC(array('Email' => $this->UserName)));  
        }else{
            $this->model = MemberModel::model()->find(BaseModel::getC(array('UserName' => $this->UserName)));                
        }
        if($this->model === false){
            $this->addError('VerifyCode', '不存在此用户');
        }
        return true;
    }


    public function checkVerify(){
        if(!yii::app()->getController()->createAction('captcha')->validate($this->VerifyCode, false)){
            $this->addError('VerifyCode', '验证码不正确');
            return false;
        }        
    }


    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'UserName' => '名称',
            'verifyCode' => '验证码',
        );
    }

}
